<?php
/**
 * 核心数据库
 */

/**
 * 获取一个model对应的table名
 * model名使用大驼峰式命名,如:UserInfoModel对应表名为user_info
 *
 * @param string $modelClassName model类名
 * @return string
 */
function get_table_name($modelClassName) {
	preg_match_all ( '/[A-Z][a-z]*/', $modelClassName, $matches );
	$tableName = '';
	foreach ( $matches [0] as $match ) {
		if ($match != 'Model') {
			$tableName .= lcfirst ( $match ) . '_';
		}
	}
	return substr ( $tableName, 0, strlen ( $tableName ) - 1 );
}

/**
 * 导入文件(有缓存功能)
 *
 * @param string $filePath 文件路径
 * @return boolean
 */
function import($filePath) {
	static $_file = array ();
	// 先判断文件是否已经被导入
	if (! isset ( $_file [$filePath] )) {
		$_file [$filePath] = true;
		require $filePath;
		return true;
	}
	return false;
}

/**
 * 返回应用的基url
 *
 * @return string
 */
function app_base_url() {
	return APP_BASE_URL;
}

/**
 * 返回当前模块的基url
 *
 * @return string
 */
function base_url() {
	return BASE_URL;
}
/**
 * 返回css资源基url
 */
function css_base_url() {
	return app_base_url () . '/public/css';
}
/**
 * 返回js资源基url
 */
function js_base_url() {
	return app_base_url () . '/public/js';
}
/**
 * 返回a标签
 *
 * @param string $url
 * @param string $text
 * @param string $class
 * @return string
 */
function tag_a($url, $text, $class = '') {
	if (! empty ( $class )) {
		return "<a class=\"$class\" href=\"{$url}\">{$text}</a>";
	}
	return "<a href=\"{$url}\">{$text}</a>";
}
/**
 * 返回li标签
 *
 * @param string $text
 * @param string $class
 * @return string
 */
function tag_li($text, $class = '') {
	if (! empty ( $class )) {
		return "<li class=\"$class\">{$text}</li>";
	}
	return "<li>{$text}</li>";
}

/**
 * 分页组件
 *
 * @param string $url
 * @param integer $curPage
 * @param integer $maxPage
 * @param integer $pageLength
 * @param array|string $params
 * @return string
 */
function pagination($url, $curPage, $maxPage, $pageLength, $params = '') {
	$url = base_url () . '/' . $url;
	// 查询字符串参数
	if (! empty ( $params )) {
		if (is_array ( $params )) {
			$params = '&' . http_build_query ( $params );
		}
		else if (is_string ( $params )) {
			$params .= '&' . $params;
		}
	}
	$html = '<ul class="pagination">';
	$start = $curPage - $pageLength;
	$end = $curPage + $pageLength;
	$start = ($start > 0) ? $start : 1;
	$end = ($end <= $maxPage) ? $end : $maxPage;
	
	if ($curPage != 1) {
		$html .= tag_li ( tag_a ( $url . '?page=' . ($curPage - 1) . $params, '上一页' ), 'prev' );
	}
	if ($start > 1) {
		$html .= tag_li ( tag_a ( $url . '?page=1' . $params, 1 ) );
		$html .= tag_li ( '<span>...</span>', 'disabled' );
	}
	
	for($index = $start; $index <= $end; $index ++) {
		if ($index != $curPage) {
			$html .= tag_li ( tag_a ( $url . '?page=' . $index . $params, $index ) );
		}
		else {
			$html .= tag_li ( tag_a ( $url . '?page=' . $index . $params, $index ), 'active' );
		}
	}
	
	if ($end < $maxPage) {
		$html .= tag_li ( '<span>...</span>', 'disabled' );
		$html . tag_li ( tag_a ( $url . '?page=' . $maxPage . $params, $maxPage ) );
	}
	if ($curPage != $maxPage) {
		$html .= tag_li ( tag_a ( $url . '?page=' . ($curPage + 1) . $params, '下一页' ), 'next' );
	}
	$html .= '</ul>';
	return $html;
}

/**
 * 快速导入第三方框架类库
 * 所有第三方框架的类库文件统一放到vendor目录下
 *
 * @param string $className 类名
 * @return boolean
 */
function vendor($className) {
	return import ( VENDOR_PATH . '/' . $className . '.php' );
}

/**
 * 发送HTTP状态
 *
 * @param integer $code 状态码
 * @return void
 */
function send_http_status($code) {
	static $_status = array (
			// Informational 1xx
			100 => 'Continue',
			101 => 'Switching Protocols',
			// Success 2xx
			200 => 'OK',
			201 => 'Created',
			202 => 'Accepted',
			203 => 'Non-Authoritative Information',
			204 => 'No Content',
			205 => 'Reset Content',
			206 => 'Partial Content',
			// Redirection 3xx
			300 => 'Multiple Choices',
			301 => 'Moved Permanently',
			302 => 'Moved Temporarily ', // 1.1
			303 => 'See Other',
			304 => 'Not Modified',
			305 => 'Use Proxy',
			// 306 is deprecated but reserved
			307 => 'Temporary Redirect',
			// Client Error 4xx
			400 => 'Bad Request',
			401 => 'Unauthorized',
			402 => 'Payment Required',
			403 => 'Forbidden',
			404 => 'Not Found',
			405 => 'Method Not Allowed',
			406 => 'Not Acceptable',
			407 => 'Proxy Authentication Required',
			408 => 'Request Timeout',
			409 => 'Conflict',
			410 => 'Gone',
			411 => 'Length Required',
			412 => 'Precondition Failed',
			413 => 'Request Entity Too Large',
			414 => 'Request-URI Too Long',
			415 => 'Unsupported Media Type',
			416 => 'Requested Range Not Satisfiable',
			417 => 'Expectation Failed',
			// Server Error 5xx
			500 => 'Internal Server Error',
			501 => 'Not Implemented',
			502 => 'Bad Gateway',
			503 => 'Service Unavailable',
			504 => 'Gateway Timeout',
			505 => 'HTTP Version Not Supported',
			509 => 'Bandwidth Limit Exceeded' 
	);
	header ( 'HTTP/1.1 ' . $code . ' ' . $_status [$code] );
	header ( 'Status:' . $code . ' ' . $_status [$code] );
}

/**
 * 格式化时间
 *
 * @param int $time 时间戳
 */
function simple_date_format($time) {
	return date ( 'Y-m-d H:i:s', $time );
}
